package com.yztstudy.controller;


import com.yztstudy.dto.Result;
import com.yztstudy.entity.Blog;
import com.yztstudy.service.IBlogService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;


/**
 * Blog的表示层
 */
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Resource
    private IBlogService blogService;


    /**
     * TODO 保存用户发表的博客
     * @param blog
     * @return
     */
    @PostMapping
    public Result saveBlog(@RequestBody Blog blog) {
        return blogService.saveBlog(blog);
    }


    /**
     * TODO 查看别人发表的博客
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result queryBlogById(@PathVariable("id") Long id)
    {
        return blogService.queryBlogById(id);
    }


    /**
     * TODO 用户点赞功能
     * @param id
     * @return
     */
    @PutMapping("/like/{id}")
    public Result likeBlog(@PathVariable("id") Long id) {
        return blogService.likeBlog(id);
    }


    /**
     * TODO 查询自己发表过的博客
     * @param current 当前页码
     * @return
     */
    @GetMapping("/of/me")
    public Result queryMyBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        return blogService.queryMyBlog(current);
    }


    /**
     * TODO 查询这个博主的所有笔记
     * @param current 当前的分页数量
     * @param id 用户的ID
     * @return
     */
    @GetMapping("/of/user")
    public Result queryOtherBlog(@RequestParam(value = "current", defaultValue = "1") Integer current, @RequestParam("id") Long id) {
        return blogService.queryOtherBlog(current, id);
    }


    /**
     * TODO 查询热点博客
     * @param current
     * @return
     */
    @GetMapping("/hot")
    public Result queryHotBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        return blogService.queryHotBlog(current);
    }


    /**
     * TODO 根据博客的ID查询点赞的人
     * @param id
     * @return
     */
    @GetMapping("/likes/{id}")
    public Result queryBlogLikes(@PathVariable("id") Long id)
    {
        return blogService.queryBlogLikes(id);
    }


    /**
     * TODO 滚动分页查询
     * @param max 上一次查询的最小时间戳
     * @param offset 偏移量
     * @return
     */
    @GetMapping("/of/follow")
    public Result queryBlogOfFollow(@RequestParam("lastId") Long max, @RequestParam(value = "offset", defaultValue = "0") Integer offset)
    {
        return blogService.queryBlogOfFollow(max, offset);
    }
}
